/**
 * Created by duanxiancai on 2016/12/28.
 */
function loadScript(url, callback){
    var script = document.createElement ("script")
    script.type = "text/javascript";
    if (script.readyState){ //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" || script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else { //Others
        script.onload = function(){
            callback();
        };
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}
function singleLoad(callBack) {
    loadScript("js/jquery.min.js",function () {
        loadScript("js/customLayout.js",function () {
            loadScript("js/jqRequest.js",function () {
                callBack();
            })
        })
    })
}

function loadPresnetList(type) {
    singleLoad(function () {
        //获取请求参数
        var params = getParams();
        //在着初始化实体类
        function PresentModel(obj) {
            this.id = obj.giftId;
            this.type = obj.gifttype;
            this.title = obj.title;
            this.date = obj.end;
        }
        var presentList = new Array();
        params["used"] = type;
        var param = {"token":params.token,"used":params.used};
        sendRequest("findAllGift",param,function (response) {
            presentList = response;
            reloadPresentList();
        });
        function reloadPresentList() {
            if (presentList == undefined||presentList.length == 0){
                $('#emptyCell').show();
            }
            $(presentList).each(function () {
                var model = new PresentModel(this);
                var imgUrl = 'img/card_wodelipin_kaquan.png';
                if (model.type == 1){//食用
                    imgUrl = 'img/card_wodelipin_shipin.png';
                }else if(model.type == 2){//卡劵
                    imgUrl = 'img/card_wodelipin_kaquan.png';
                }else if(model.type == 3){//生活
                    imgUrl = 'img/card_wodelipin_shenghuoyongpin.png';
                }

                var cell = '<li class="mui-table-view-cell"><div class="presentContentView"><img class="present_bgImg" src="'+imgUrl+'"><div class="presentTitle">'+model.title+'</div> <div class="presentTime">兑换有效期至:'+formateDate(model.date)+'</div></div></li>';
                $("ul").append(cell);
            });
            loadPresentCSS(type);
        }
        function loadPresentCSS(type) {
            $(function (){
                if (type == 1){
                    $("ul").on("click","li",function(){
                        var index = $(this).index();
                        var obj = presentList[index-1];
                        self.location.href="xfxs://web/presentDetail.html?giftId="+obj.giftId;
                    });
                }
                $(".presentContentView").css({
                    "left":getPxAutoWidth(12),
                    "width":getPxAutoWidth(351),
                    "height":getPxAutoWidth(140)
                });
                $(".historyPresentCell").css({
                    "height": getPXAutoHeight(30),
                    "font-size": getPXAutoHeight(12),
                    "line-height": getPXAutoHeight(30)
                })
            });
        };
    })
}
function loadSuccess() {
    singleLoad(function () {
        var times = 0;
        $(function () {
            $(".smileContent").css({
                "height":getPxAutoWidth(250)
            });
            $(".smileImg").css({
                "margin-left": getPxAutoWidth(120),
                "width": getPxAutoWidth(135),
                "height": getPxAutoWidth(135),
                "margin-top": getPxAutoWidth(23)
            });
            $(".smiletitle").css({
                "margin-top":getPxAutoWidth(6)
            });
            $(".smileJump").css({
                "margin-top":getPxAutoWidth(30)
            });
            $(".gglContent").css({
                "height": getPxAutoWidth(150),
                "margin-top": getPxAutoWidth(10)
            });
            $(".gglContent img").css({
                "width": getPxAutoWidth(351),
                "top": getPxAutoWidth(12),
                "left": getPxAutoWidth(12)
            });
            $(".ggl").css({
                "width": getPxAutoWidth(341),
                "height": getPxAutoWidth(116),
                "top": getPxAutoWidth(17),
                "left": getPxAutoWidth(17)
            });
            $(".timesContent").css({
                "height": getPxAutoWidth(22),
                "margin-top": getPxAutoWidth(16)
            });
            $(".timesBgImg").css({
                "width":getPxAutoWidth(91),
                "right":getPxAutoWidth(12)
            });
            $(".times").css({
                "right": getPxAutoWidth(27),
                "width": getPxAutoWidth(77.5),
                "line-height": getPxAutoWidth(22),
                "font-size": getPxAutoWidth(10)
            });
            $(".detail1").css({
                "font-size": getPxAutoWidth(12),
                "height": getPxAutoWidth(14),
                "margin-top": getPxAutoWidth(32)
            });
            $(".detail2").css({
                "font-size": getPxAutoWidth(10),
                "height": getPxAutoWidth(12),
                "margin-top": getPxAutoWidth(18)
            });
            $(".detail3").css({
                "font-size": getPxAutoWidth(10),
                "height": getPxAutoWidth(12),
            });
            $(".detailTitle").css({
                "font-size": getPxAutoWidth(22),
                "height": getPxAutoWidth(24),
                "margin-top": getPxAutoWidth(18)
            });
        });
        //获取请求参数
        var params = getParams();
        //进入页面先获取缴费结果和刮刮乐相关信息
        var isShowGGL = true;
        if (params.giftId == undefined && params.giftId == null){
            isShowGGL = false;
        }
        console.info(isShowGGL);
        console.info(params.giftId);
        if (isShowGGL){
            loadScript("js/wScratchPad.js",function () {
                window.onload = function () {
                    var gglWidth=parseFloat(getPxAutoWidth(341));
                    var gglHeight=parseFloat(getPxAutoWidth(116));
                    $("#wScratchPad2").wScratchPad({
                        width:gglWidth,
                        height:gglHeight,
                        realtime:true,
                        scratchMove: function (e, percent) {
                            if (percent>30&percent<100){
                                this.clear();
                                //发送该奖品已刮的接口
                                var param = {"giftId":params.giftId};
                                sendRequest("updateGiftByShave",param,function (response) {
                                    $(".times").html('已中奖<span style="color: #ff5757">'+(times+1)+'</span>次');
                                });
                            }
                        }
                    })
                };
                var param = {"giftId":params.giftId};
                sendRequest("findGiftByLoad",param,function (response) {
                    times = parseFloat(response.getPrize);
                    $(".detailTitle").html(response.title);
                    var days = (response.end-Date.parse(new Date()))/(3600*24*1000);
                    $(".detail2").html('奖品将在'+Math.floor(days)+'天后过期');
                    $(".times").html('已中奖<span style="color: #ff5757">'+times+'</span>次');
                    $('div').show();
                    if (response.isShave == 1){
                        $('.ggl').hide();
                    }

                });
            });
        }else {
            $('body').css("background-color","#FFFFFF");
        }
    })
}

function loadPresentDetail() {
    singleLoad(function () {
        //获取请求参数
        var params = getParams();
        $(function (){
            $(".bgImg").css({
                "top":getPxAutoWidth(45),
                "left": getPxAutoWidth(12),
                "width":getPxAutoWidth(351)
            });
            $(".present-icon").css({
                "width":getPxAutoWidth(46),
                "top":getPxAutoWidth(22),
                "left": getPxAutoWidth(164.5)
            });
            $(".titleContent").css({
                "top":getPxAutoWidth(68),
                "left": getPxAutoWidth(24),
                "width":getPxAutoWidth(327),
                "height":getPxAutoWidth(154)
            });
            $(".presentTitle").css({
                "margin-top":getPxAutoWidth(23),
                "height":getPxAutoWidth(18)
            });
            $(".presentImg").css({
                "margin-top":getPxAutoWidth(18),
                "height":getPxAutoWidth(86)
            });
            $(".line").css({
                "top":getPxAutoWidth(222),
                "left":getPxAutoWidth(24),
                "width":getPxAutoWidth(330)
            });
            $(".detailContentView").css({
                "top":getPxAutoWidth(224),
                "height":getPxAutoWidth(172)
            });
            $(".alert").css({
                "margin-top":getPxAutoWidth(16)
            });
            $(".codeMark").css({
                "margin-top":getPxAutoWidth(33)
            });
            $(".code").css({
                "margin-top":getPxAutoWidth(20)
            });
            $(".date").css({
                "margin-top":getPxAutoWidth(20)
            });
            $(".specialAlert").css({
                "top": getPxAutoWidth(410),
                "height": getPxAutoWidth(47),
                "line-height": getPxAutoWidth(47)
            });
            $("#bottomLine").css({
                "top":getPxAutoWidth(409)
            });
        });
        //请求数据
        var param = {"giftId":params.giftId};
        sendRequest("findGiftById",param,function (response) {
            $(".presentTitle").html(response.title);
            $(".code").html(addCenterSpace(response.redeem));
            $(".date").html('兑换有效期至:'+formateDate(response.end));
        });
    })
}